#include "DGame.h"
#include "core/utils.h"
#include "core/base_scene.h"

USING_NS_CC;

DGame::DGame()
    :_pScene(nullptr)
    ,_nTick(0)
    ,_fDisplayWidth(0.0f)
    ,_fDisplayHeight(0.0f)
{
}

DGame::~DGame()
{
}

DGame* DGame::create(float width_, float height_)
{
    auto pRet = new DGame();
    if ( pRet && pRet->init(width_, height_) )
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return nullptr;
}

bool DGame::init(float width_, float height_)
{
    if ( Layer::init() )
    {
        _fDisplayWidth = width_;
        _fDisplayHeight = height_;

        return true;
    }
    return false;
}

void DGame::setup(int sceneId_)
{
    if ( !_pScene ) _pScene = new DScene();
    
    
    _pScene->init();
	_pScene->setup(1);
    addChild(_pScene->getStage());

    if ( true )
    {
        play();
    }
    else
    {
        /*auto pSceneReadyListener = EventListenerCustom::create("onSceneReady", [=](EventCustom* event){
		onEquipItemClick(event->getUserData());
	    });
        getEventDispatcher()->addEventListener(pSceneReadyListener);
        getEventDispatcher()->removeEventListener(*/
    }
}

void DGame::render(float delta_)
{
    _nTick = TimeUtil::getTimer();
    if ( _pScene )
        _pScene->render(_nTick);
}

void DGame::play()
{
    schedule(schedule_selector(DGame::render));
}

void DGame::stop()
{
    unschedule(schedule_selector(DGame::render));
}